package com.android.pagemarket.ui.widget;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.android.pagemarket.R;
import com.android.pagemarket.io.protocol.in.TransQueryAction;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-1
 * @see : 物流条件查询弹出框
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class TransQueryPopWindow extends PopupWindow
implements OnClickListener,PopupWindow.OnDismissListener
{
    Context mContext;
    private EditText order_num;
    private EditText paperType;
    private EditText degree;
    private Button   btn_search_order;
    private DatePickerDialog datePickerDialog;
    private EditText datePickerEditer;
    
    private TransQueryAction mAction;
    private LoadableView     mLoadingView;
    private boolean isSetData = false;
    private Calendar cal;
    
    public TransQueryPopWindow(Context context)
    {
        super(LayoutInflater.from(context).inflate(R.layout.popview_transquery, null),
               LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mContext = context;
        setBackgroundDrawable(new ColorDrawable(0x99000000));
        setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        setOnDismissListener(this);
        
        initViews();
    }
    
    public void setAction(TransQueryAction action, LoadableView loadView)
    {
        mAction = action;
        mLoadingView = loadView;
    }
    
    private void initViews()
    {
        View v = getContentView();
        order_num = (EditText)v.findViewById(R.id.order_num);
        paperType = (EditText)v.findViewById(R.id.paperType);
        degree = (EditText)v.findViewById(R.id.degree);
        btn_search_order = (Button)v.findViewById(R.id.btn_search_order);
        btn_search_order.setOnClickListener(this);
        
        cal = Calendar.getInstance();
        
        datePickerEditer = (EditText)v.findViewById(R.id.datePickerEditer);
        datePickerEditer.setInputType(InputType.TYPE_NULL);
        datePickerEditer.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if(hasFocus)
                {
                    showDataDialog();
                }
            }
        });
        datePickerEditer.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_search_order:
            {
                String numStr = order_num.getEditableText().toString();
                String paperStr = paperType.getEditableText().toString();
                String degreeStr = degree.getEditableText().toString();
                String dataStr = null;
                
                if(isSetData)
                {
                    dataStr = datePickerEditer.getEditableText().toString();
                }                
                if(!TextUtils.isEmpty(dataStr))
                {
                    mAction.setDate(dataStr);
                }else
                {
                    mAction.setDate(null);
                }
                
                if(!TextUtils.isEmpty(degreeStr))
                {           
                    mAction.setDegree(Integer.parseInt(degreeStr));
                }else
                {
                    mAction.setDegree(-1);
                }
                
                if(!TextUtils.isEmpty(numStr))
                {
                    mAction.setOId(Integer.parseInt(numStr));
                }else
                {
                    mAction.setOId(-1);
                }
                        
                if(!TextUtils.isEmpty(paperStr))
                {
                    mAction.setPaperType(paperStr);
                }else
                {
                    mAction.setPaperType(null);
                }
                dismiss();
                // 条件改变时，重置
//                mAction.setPage(0, 10);
                mLoadingView.showLoadingView();
                mAction.submit();
                break;
            }
            case R.id.datePickerEditer:
            {
                showDataDialog();
                break;
            }
        }
    }
    
    @Override
    public void onDismiss()
    {
        if(datePickerDialog != null && datePickerDialog.isShowing())
        {
            datePickerDialog.dismiss();
            datePickerDialog = null;
        }
    }
    
    private void showDataDialog()
    {
        if(datePickerDialog == null)
        {
            datePickerDialog = new DatePickerDialog(mContext,new OnDateSetListener()
            {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth)
                {
                    isSetData = true;
                    datePickerEditer.setText(year + "-" + (monthOfYear+1) + "-" + dayOfMonth);
                }
            },
            cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));          
            datePickerDialog.setOnCancelListener(new OnCancelListener()
            {
                @Override
                public void onCancel(DialogInterface dialog)
                {
                    isSetData = false;
                    datePickerEditer.setText("");
                }
            });
            datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,mContext.getString(R.string.cancel), 
                    new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    isSetData = false;
                    datePickerEditer.setText("");
                }
            });
        }
        datePickerDialog.show();
    }
}